﻿using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CrabSS
{
    /// <summary>
    /// AddCore.xaml 的交互逻辑
    /// </summary>
    public partial class AddCore : MetroWindow
    {
        public AddCore()
        {
            InitializeComponent();
        }
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MainWindow mw = new();
            mw.data.Content = "内核未导入:(";
        }

        private async void choosejvav_Click(object sender, RoutedEventArgs e)
        {
            var of = new OpenFileDialog();
            of.Filter = "jar file|*.jar";
            if (of.ShowDialog() == true)
            {
                try
                {
                    string corePath = System.IO.Path.GetFullPath(of.FileName);
                    string coreName = of.SafeFileName;
                    File.Copy(corePath, Environment.CurrentDirectory + "\\start.jar");
                    jvav.Text = coreName;
                }
                catch (Exception ex)
                {
                    await this.ShowMessageAsync(":(", ex.ToString(), MessageDialogStyle.AffirmativeAndNegative);
                }            
            }
        }

        private async void verify_Click(object sender, RoutedEventArgs e)
        {
            if (File.Exists("start.jar"))
            {
                this.Close();
                MainWindow mw = new();
                mw.data.Content = "正在启动服务器";
                mw.start.IsEnabled = false;
                mw.start.Content = "服务器已启动";
                string result = GetRandomString(5, true, true, true, false, "temp");
                string cmd = "start.cmd";
                FileStream fs = File.Create(cmd);
                if(mw.jvav.Text == "")
                {
                    if (mw.customcode.Text == "")
                    {
                        string eula = "java -Xms" + mw.min.Value + "m -Xmx" + mw.max.Value + "m -jar start.jar -nogui \npause";
                        byte[] eulaB = System.Text.Encoding.Default.GetBytes(eula);
                        fs.Write(eulaB, 0, eulaB.Length);
                        fs.Close();
                    }
                    else
                    {
                        string eula = "java -Xms" + mw.min.Value + "m -Xmx" + mw.max.Value + "m -jar start.jar " + mw.customcode.Text + "\npause";
                        byte[] eulaB = System.Text.Encoding.Default.GetBytes(eula);
                        fs.Write(eulaB, 0, eulaB.Length);
                        fs.Close();
                    }
                }
                else
                {
                    if (mw.customcode.Text == "")
                    {
                        string eula = mw.jvav.Text+" -Xms" + mw.min.Value + "m -Xmx" + mw.max.Value + "m -jar start.jar -nogui \npause";
                        byte[] eulaB = System.Text.Encoding.Default.GetBytes(eula);
                        fs.Write(eulaB, 0, eulaB.Length);
                        fs.Close();
                    }
                    else
                    {
                        string eula = mw.jvav.Text + " -Xms" + mw.min.Value + "m -Xmx" + mw.max.Value + "m -jar start.jar " + mw.customcode.Text + "\npause";
                        byte[] eulaB = System.Text.Encoding.Default.GetBytes(eula);
                        fs.Write(eulaB, 0, eulaB.Length);
                        fs.Close();
                    }
                }
                console cons = new();
                cons.Show();
            }
            else
            {
                await this.ShowMessageAsync(":(", "您的内核没了捏（", MessageDialogStyle.AffirmativeAndNegative);
            }
        }

        public static string GetRandomString(int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom)
        {
            byte[] b = new byte[4];
            new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b);
            Random r = new Random(BitConverter.ToInt32(b, 0));
            string s = null, str = custom;
            if (useNum == true) { str += "0123456789"; }
            if (useLow == true) { str += "abcdefghijklmnopqrstuvwxyz"; }
            if (useUpp == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
            if (useSpe == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; }
            for (int i = 0; i < length; i++)
            {
                s += str.Substring(r.Next(0, str.Length - 1), 1);
            }
            return s;
        }
    }
}
